package Hot100;

import java.util.LinkedList;
import java.util.Stack;

public class p20 {
    class Solution {
        public boolean isValid(String s) {
            if (s.length() < 2 || s.length() % 2 != 0) {
                return false;
            }
            char[] chStr = s.toCharArray();
            LinkedList<Character> list = new LinkedList<>();
            Character last;
            list.add(chStr[0]);
            for (int i = 1; i < s.length(); i++) {
                if (!list.isEmpty()){
                    last = list.getLast();
                    if (last == '(' && chStr[i] == ')' || last == '[' && chStr[i] == ']' || last == '{' && chStr[i] == '}'){
                        list.removeLast();
                        continue;
                    }
                }
                list.add(chStr[i]);
            }
            return list.size() == 0;
        }
    }
}
